﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EIS.DatabaseHelper;
using EIS.Data;
using EIS.Obj;


namespace EIS.ConfigViews
{
    public partial class FunctionViewLayout : Form
    {
        private string ID;
        private string schemaTableName;
        private DataSet ds;
        private DataSet dsSelected;
        public FunctionViewLayout(string functionID)
        {
 
            InitializeComponent();
            ID = functionID;
            SetData(functionID);
            
        }

        private void FunctionViewLayout_Load(object sender, EventArgs e)
        {
        }

        private void listBoxLeft_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void SetData(string functionID)
        {
            var dbHandler = new DBHandler();
            schemaTableName = new FunctionSchema(functionID).SchemaTableName;
            ds = dbHandler.SelectSimple(schemaTableName);
            //Remove EISSYSMAINID
            //TODO Java not to send EISSYSMAINID as schema
            ds.Tables[0].Rows.RemoveAt(0);
            this.listBoxLeft.DataSource = ds.Tables[0].DefaultView;
            this.listBoxLeft.DisplayMember = ds.Tables[0].Columns[1].ColumnName;

            dsSelected = ds.Clone();
            this.listBoxRight.DataSource = dsSelected.Tables[0].DefaultView;
            this.listBoxRight.DisplayMember = dsSelected.Tables[0].Columns[1].ColumnName;

            DataSet dsCurrentSelectedInDatabase = dbHandler.SelectSimple(EISConstant.GetLayoutTableName(ID));
            foreach ( DataRow drCurrent  in dsCurrentSelectedInDatabase.Tables[0].Rows)
            {
                foreach (DataRow drInDs in ds.Tables[0].Rows)
                {
                    if (drCurrent[0].ToString() == drInDs[0].ToString())
                    {
                        var drToAdd = dsSelected.Tables[0].NewRow();
                        drToAdd.ItemArray = drInDs.ItemArray;
                        ds.Tables[0].Rows.Remove(drInDs);
                        dsSelected.Tables[0].Rows.Add(drToAdd);
                        break;
                        
                    }
                }
            }
        }

        private void btnToRight_Click(object sender, EventArgs e)
        {
            foreach (int index in listBoxLeft.SelectedIndices)
            {
                DataRow dr = dsSelected.Tables[0].NewRow();
                dr.ItemArray = ds.Tables[0].Rows[index].ItemArray;

                dsSelected.Tables[0].Rows.Add(dr);
            }

            foreach (int index in listBoxLeft.SelectedIndices)
            {
                ds.Tables[0].Rows.RemoveAt(index);
            }
        }

        private void btnToLeft_Click(object sender, EventArgs e)
        {
            foreach (int index in listBoxRight.SelectedIndices)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr.ItemArray = dsSelected.Tables[0].Rows[index].ItemArray;

                ds.Tables[0].Rows.Add(dr);
            }

            foreach (int index in listBoxRight.SelectedIndices)
            {
                dsSelected.Tables[0].Rows.RemoveAt(index);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var dbHandler = new DBHandler();
            string layoutTableName = EISConstant.GetLayoutTableName(ID);
            DataSet dsLayout = dbHandler.GetEmptyDataSet(layoutTableName);
            for (int i = 0; i < dsSelected.Tables[0].Rows.Count; i++)
            {
                var drToSave = dsLayout.Tables[0].NewRow();
                drToSave["FieldName"] = dsSelected.Tables[0].Rows[i][0].ToString();
                drToSave["ColumPosition"] = i;
                dsLayout.Tables[0].Rows.Add(drToSave);
            }

            dbHandler.DeleteAllRecordsInTable(layoutTableName);

            dbHandler.InsertRecords(layoutTableName, dsLayout.Tables[0]);
            this.Close();
        }

    }
}
